
package com.example.chiro;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Spinner;
import android.widget.TextView;

public class Adaptor_espece_item extends ArrayAdapter<Donnees_espece>{
	//Context context;
		int layoutResourceId;
		Holder holder;
		ArrayList<Donnees_espece> liste;
		private LayoutInflater mInflater;
		private Context context;
		

		// TODO Auto-generated constructor stub
		public Adaptor_espece_item(Context context, int layoutResourceId,
				ArrayList<Donnees_espece> data) {
			super(context, layoutResourceId, data);
			this.layoutResourceId = layoutResourceId;
			this.context = context;
			mInflater = LayoutInflater.from(context);
			this.liste = data ;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {

				
				

			View view = null;
			if (convertView == null){
				LayoutInflater inflate = ((Activity) context).getLayoutInflater();
				view = inflate.inflate(R.layout.slide_item_espece, null);
				final Holder ViewHolder = new Holder();
				
				ViewHolder.rarete = (Spinner) view.findViewById(R.id.slide_espece_rarete);
				ArrayAdapter<CharSequence> adapter_type = ArrayAdapter.createFromResource(mInflater.getContext(), R.array.espece_rarete,android.R.layout.simple_spinner_item);
				adapter_type.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				ViewHolder.rarete.setAdapter(adapter_type);
				
				ViewHolder.rarete.setOnItemSelectedListener(new OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						// TODO Auto-generated method stub
						Donnees_espece element = (Donnees_espece)ViewHolder.rarete.getTag();
						element.setSpin_rarete_selected(arg0.getSelectedItemPosition());
						//element.setT_rarete(arg0.getItemAtPosition(arg2).toString());
						
						if (arg2 != 0)
							element.setT_rarete(String_operation.op_string(arg0.getItemAtPosition(arg2).toString()));
						else 
							element.setT_rarete("NC");
					}

					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
						// TODO Auto-generated method stub
						
					}
				});
				
				ViewHolder.nom = (EditText)view.findViewById(R.id.slide_espece_nom);
				ViewHolder.nom.addTextChangedListener(new TextWatcher() {
					
					@Override
					public void onTextChanged(CharSequence s, int start, int before, int count) {
					}
					
					@Override
					public void beforeTextChanged(CharSequence s, int start, int count,
							int after) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void afterTextChanged(Editable s) {
						// TODO Auto-generated method stub
						Donnees_espece element = (Donnees_espece)ViewHolder.nom.getTag();
						element.setT_nom(s.toString());
					}
				});
				
				ViewHolder.effectif = (EditText)view.findViewById(R.id.slide_espece_effectif);
				ViewHolder.effectif.addTextChangedListener(new TextWatcher() {
					
					@Override
					public void onTextChanged(CharSequence s, int start, int before, int count) {
					}
					
					@Override
					public void beforeTextChanged(CharSequence s, int start, int count,
							int after) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void afterTextChanged(Editable s) {
						// TODO Auto-generated method stub
						Donnees_espece element = (Donnees_espece)ViewHolder.effectif.getTag();
						element.setT_effectif(s.toString());
					}
				});
				
				ViewHolder.commentaire = (EditText)view.findViewById(R.id.slide_espece_commentaire);
				ViewHolder.commentaire.addTextChangedListener(new TextWatcher() {
					
					@Override
					public void onTextChanged(CharSequence s, int start, int before, int count) {
					}
					
					@Override
					public void beforeTextChanged(CharSequence s, int start, int count,
							int after) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void afterTextChanged(Editable s) {
						// TODO Auto-generated method stub
						Donnees_espece element = (Donnees_espece)ViewHolder.commentaire.getTag();
						element.setT_commentaire(s.toString());
					}
				});
				
				
				view.setTag(ViewHolder);
				//ViewHolder.num_espece.setTag(liste.get(position));
				ViewHolder.rarete.setTag(liste.get(position));
				ViewHolder.nom.setTag(liste.get(position));
				ViewHolder.effectif.setTag(liste.get(position));
				ViewHolder.commentaire.setTag(liste.get(position));
				
				
			}else{
				//holder = (Holder) row.getTag();
				view = convertView;
				//((Holder)view.getTag()).num_espece.setTag(liste.get(position));
				((Holder)view.getTag()).rarete.setTag(liste.get(position));
				((Holder)view.getTag()).nom.setTag(liste.get(position));
				((Holder)view.getTag()).effectif.setTag(liste.get(position));
				((Holder)view.getTag()).commentaire.setTag(liste.get(position));
				
			}
			
			Holder holder = (Holder)view.getTag();
			//holder.num_espece.setText("Cavité : "+liste.get(position).getNum_espece());
			holder.rarete.setSelection(liste.get(position).getSpin_rarete_selected());
			holder.nom.setText(liste.get(position).getT_nom());
			holder.effectif.setText(liste.get(position).getT_effectif());
			holder.commentaire.setText(liste.get(position).getT_commentaire());
			
			//holder.t_type = liste.get(position).getType();
			TextView numero = (TextView)view.findViewById(R.id.num_espece);
			numero.setText("Espèce : "+liste.get(position).getNum_espece()+"/100");
			
			/*
			if (liste.get(position).getNum_espece()%2 == 0){
				view.setBackgroundColor(Color.LTGRAY);
			}
			*/
			return view;
			
		}

		static class Holder {
			Spinner rarete;
			EditText nom, effectif, commentaire;
			
			String photo_path;

			
		}
}
